﻿using System.Windows;
using System.Windows.Input;
using HandyControl.Controls;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using SoonCode.Core;
using SoonCode.FocusTest.Api;
using SoonCode.FocusTest.Models;
using MessageBox = HandyControl.Controls.MessageBox;

namespace SoonCode.FocusTest.ViewModels
{
    class MemberFeeEditViewModel:BindableBase,IDialogAware
    {
        private string memberId;
        private string id;
        public ICommand CloseDialogCommand { get; set; }
        public ICommand SaveDataCommand { get; set; }
        public string Name { get; set; }
        public double Money { get; set; }
        public string Tollman { get; set; }
        public string Notes { get; set; }
        public MemberFeeEditViewModel()
        {
            CloseDialogCommand = new DelegateCommand(() => RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)));
            SaveDataCommand = new DelegateCommand(SaveData);
        }

        private async void SaveData()
        {
            if (this.Name.IsNullOrEmpty() || this.Money.IsNullOrEmpty() || this.Tollman.IsNullOrEmpty())
            {
                MessageBox.Show("数据填写不完整，请完善数据后再试", "数据错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            FeeInfo fee = new FeeInfo();
            fee.id = this.id;
            fee.name = Name;
            fee.money = Money;
            fee.tollman= Tollman;
            fee.notes = Notes;
            fee.memberId = memberId;
            bool result = await API.SaveFee(fee);
            if (result)
            {
                RequestClose.Invoke(new DialogResult(ButtonResult.OK));
            }
        }
        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            FeeInfo info = parameters.GetValue<FeeInfo>("fee");
            this.Name = info.name;
            this.Money = info.money;
            this.Notes = info.notes;
            this.Tollman= info.tollman;
            this.memberId=info.memberId;
            this.id=info.id;
        }

        public string Title { get; } = "编辑数据";
        public event Action<IDialogResult>? RequestClose;
    }
}
